package com.xuecheng.manage_course.service;

import com.xuecheng.framework.domain.course.CoursePic;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_course.dao.CoursePicRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CoursePicService {

    @Autowired
    private CoursePicRepository coursePicRepository;

    /**
     * 新增课程图片
     * @param courseId
     * @param pic fastDFS中存储图片fileId
     * @return
     */
    public ResponseResult addCoursePic(String courseId, String pic) {
        CoursePic coursePic = new CoursePic();
        coursePic.setCourseid(courseId);
        coursePic.setPic(pic);
        coursePicRepository.save(coursePic);
        return new ResponseResult(CommonCode.SUCCESS);
    }


    /**
     * 查询课程图片
     * @param courseId
     * @return
     */
    public CoursePic findCoursePic(String courseId) {
        Optional<CoursePic> coursePicOptional = coursePicRepository.findById(courseId);
        return coursePicOptional.orElse(null);
    }

    /**
     * 根据id删除图片
     * @param courseId
     * @return
     */
    public ResponseResult deleteById(String courseId) {
        coursePicRepository.deleteById(courseId);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
